const SPLIT = "-";
class Invoke {
  constructor() {
    const hasInvokeChannel = wx.canIUse("getInvokeChannel");
    if (hasInvokeChannel) {
      this.channel = wx.getInvokeChannel();
    } else {
      this.channel = null;
      console.error(`Do NOT support invoke-channel!`);
    }
    this.invokeChannelWithModule = this.invokeChannelWithModule.bind(this);
    this.invokeChannel = this.invokeChannel.bind(this);
    this.registerEvent = this.registerEvent.bind(this);
    this.removeEvent = this.removeEvent.bind(this);
    this.getCommandWithModule = this.getCommandWithModule.bind(this);
  }
  /**
   * 调用channel接口
   * @param command 命令
   * @param data 数据
   * @param onSucc 成功回调
   * @param onFail 失败回调
   * */
  invokeChannel(command, data = {}, onSucc, onFail) {
    if (this.channel) {
      this.channel.invoke({
        command,
        data,
        success(res) {
          console.log(`invokeChannel:${command} success`,JSON.stringify(res));
          onSucc(res);
        },

        fail(err) {
          console.log(`invokeChannel:${command} fail:${JSON.stringify(err)}`);
          onFail(err);
        },
      });
    }
  }
  /**
   * 调用channel接口
   * @param moduleName 模块名
   * @param method 方法名
   * @param data 数据
   * @param onSucc 成功回调
   * @param onFail 失败回调
   * */
  invokeChannelWithModule(moduleName, method, data = {}, onSucc, onFail) {
    if (this.channel) {
      // const command = moduleName + SPLIT + method;
      const command = this.getCommandWithModule(moduleName, method);
      console.log(`invoke:${command}`);
      this.channel.invoke({
        command,
        data,
        success(res) {
          console.log(
            "invokeChannelWithModule success.",
            `${moduleName}-${method}`,
            "res:",
            `${res}`,
            "onSucc:",
            `${onSucc}`
          );
          if (onSucc) onSucc(res);
        },
        fail(err) {
          console.log(
            "invokeChannelWithModule fail.",
            `${moduleName}-${method}`,
            "err:",
            err
          );
          if (onFail) onFail(err);
        },
      });
    }
  }
  /**
   * 注册channel事件并监听回调
   * @param eventName:事件
   * @param onSucc:注册成功回调
   * @param onFail:注册失败回调
   * @param onEvent:收到事件的回调
   */
  registerEvent(eventName, onEvent, onSucc, onFail) {
    console.log(`registerEvent:${eventName}`);
    if (this.channel) {
      const { channel } = this;
      channel.registerEvent({
        event: eventName,
        success(res) {
          console.log(`registerEvent:${eventName} success:${JSON.stringify(err)}`);
          if (typeof onSucc === "function") {
            onSucc(res);
          }
        },
        fail(err) {
          console.log(`registerEvent:${eventName} fail:${JSON.stringify(err)}`);
          if (typeof onFail === "function") {
            onFail(err);
          }
        },
      });
      channel.on(eventName, (res) => {
        console.log(`onEvent:${eventName} res:${JSON.stringify(res)}`);
        // 回调事件
        if (typeof onEvent === "function") {
          onEvent(res);
        }
      });
    }
  }
  /**
   * 反注册channel事件
   * @param eventName:事件
   * */
  removeEvent(eventName) {
    if (this.channel) {
      console.log(`removeEventListener:${eventName}`);
      this.channel.unregisterEvent({
        event: eventName,
      });
    }
  }
  getCommandWithModule(moduleName, command) {
    return `${moduleName}${SPLIT}${command}`;
  }
}
const INVOKE = new Invoke();
const invokeChannel = INVOKE.invokeChannel;
const invokeChannelWithModule = INVOKE.invokeChannelWithModule;
const registerEvent = INVOKE.registerEvent;
const removeEvent = INVOKE.removeEvent;
const getCommandWithModule = INVOKE.getCommandWithModule;
export {
  invokeChannel,
  invokeChannelWithModule,
  registerEvent,
  removeEvent,
  getCommandWithModule,
  INVOKE,
};
